package com.ziyun.pop.web.modules.goods.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import com.utility.JsonUtils;
import com.utility.New;
import com.utility.http.HttpUtils;
import com.ziyun.pop.common.utils.UserUtils;
import com.ziyun.pop.modules.goods.entity.GoodsBrandEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 *
 *
 * @author
 * @email
 * @date 2018-08-20 09:57:04
 */
@RestController
@RequestMapping("goods/goodsbrand")
public class GoodsBrandController {


    @Value("${erpApi}")
    private String erpApi ;


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("goods:goodsbrand:list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("seller_id",UserUtils.getCurentUser().getSellerId());
//        PageUtils page = goodsBrandService.queryPage(params);
        String path = "/goods/goodsbrand/list";
        Map<String, Object> resultMap = remoteProcedureCall(params, path);
        return R.ok().put("page", resultMap.get("page"));
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    @RequiresPermissions("goods:goodsbrand:info")
    public R info(@PathVariable("brandId") Integer brandId){
        String path = "/goods/goodsbrand/info";
        Map<String, Object> map = New.hashMap();
        map.put("brandId",brandId);
        Map<String, Object> resultMap = remoteProcedureCall(map, path);
//        GoodsBrandEntity goodsBrand = goodsBrandService.selectById(brandId);
        return R.ok().put("goodsBrand", resultMap.get("goodsBrand"));
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("goods:goodsbrand:save")
    public R save(@RequestBody GoodsBrandEntity goodsBrand){
        goodsBrand.setCreateName(UserUtils.getCurentUser().getUsername());
        goodsBrand.setSellerId(Integer.parseInt(UserUtils.getCurentUser().getSellerId().toString()));
//        goodsBrandService.insert(goodsBrand);
        String path = "/goods/goodsbrand/save";
        Map<String, Object> map = New.hashMap();
        map.put("goodsBrand",goodsBrand);
        remoteProcedureCall(map, path);
        return R.ok();
    }
//
    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("goods:goodsbrand:update")
    public R update(@RequestBody GoodsBrandEntity goodsBrand){
//        ValidatorUtils.validateEntity(goodsBrand);
//        goodsBrandService.updateAllColumnById(goodsBrand);//全部更新
        goodsBrand.setLastModify(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
        String path = "/goods/goodsbrand/update";
        Map<String, Object> map = New.hashMap();
        map.put("goodsBrand",goodsBrand);
        remoteProcedureCall(map, path);
        return R.ok();
    }
//
    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("goods:goodsbrand:delete")
    public R delete(@RequestBody Integer[] brandIds){
//        goodsBrandService.deleteBatchIds(Arrays.asList(brandIds));
        return R.ok();
    }
//
    /**
     * 全部品牌
     */
    @RequestMapping("/allList")
    public R allList(@RequestParam Map<String, Object> params){
        // 远程调用
        String url = erpApi + "/goods/goodsbrand/allList";
        params.put("seller_id",UserUtils.getCurentUser().getSellerId());
        Map<String, Object> paramHeaderMap = New.hashMap();
        Map<String, Object> paramBodyMap = New.hashMap();
        paramBodyMap.put("params", params);
        // 商品列表远程调用
        String response = HttpUtils.doPost(url, paramBodyMap);
        System.out.println(response);
        Map<String, Object> map = com.ziyun.pop.common.utils.JsonUtils.jsonToMap(response);
        return R.ok().put("brandEntityList", map.get("brandEntityList"));
    }

    /**
     * 远程调用方法封装
     * @param params
     * @return
     */
    private Map<String, Object> remoteProcedureCall(@RequestParam Map<String, Object> params,String path) {
        String url = erpApi + path;
        Map<String, Object> paramHeaderMap = New.hashMap();
        Map<String, Object> paramBodyMap = New.hashMap();
        paramBodyMap.put("params",params);
        String response = HttpUtils.doPost(url, paramHeaderMap, JsonUtils.map2String(paramBodyMap));
        return com.ziyun.pop.common.utils.JsonUtils.jsonToMap(response);
    }

}
